package 剑指offer.Month7_07.day16;

public class 数字在升序数组中出现的次数 {
    public int GetNumberOfK(int [] array , int k) {
        int left = 0;
        int right = array.length - 1;
        while(left <= right){
            int mid = left + ((right - left) >> 1);
            if(array[mid] > k){
                right = mid - 1;
            }else if(array[mid] < k){
                left = mid + 1;
            }else{
                while(array[left] != k){
                    left++;
                }
                while(array[right] != k){
                    right--;
                }
                break;
            }
        }
        return right - left + 1;
    }
}
